package com.zld.smarket.service.serviceImpl;

import com.zld.smarket.dao.ShelvesMapper;
import com.zld.smarket.dto.GoodsList;
import com.zld.smarket.pojo.Goods;
import com.zld.smarket.pojo.Shelves;
import com.zld.smarket.pojo.Storehouse;
import com.zld.smarket.pojo.Supplier;
import com.zld.smarket.result.ExceptionMsg;
import com.zld.smarket.result.ResponseData;
import com.zld.smarket.service.ShelvesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
public class ShelvesServiceImpl implements ShelvesService {
    @Autowired
    private ShelvesMapper shelvesMapper;

    @Override
    public ResponseData findList(Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<Shelves> shelvesList = shelvesMapper.selectAllAndPage(page,limit);
        Long count = shelvesMapper.countShelves();
        return new ResponseData(shelvesList,count);
    }

    @Override
    public Integer deleteById(Long[] ids) {
        int j=0;
        try {
            //先判定数据是否存在
            for (Long id : ids) {
                Shelves shelves = shelvesMapper.selectByPrimaryKey(id);
                if(shelves != null) {
                    j = shelvesMapper.deleteByPrimaryKey(id);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
//            设置手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return j;
    }

    @Override
    public ResponseData search(String s, Integer page, Integer limit) {
        if(page != null && limit!=null){
            page = (page-1)*limit;
        }else{
            page = 0;
            limit = 10;
        }
        List<Shelves> shelvesList =  shelvesMapper.search(s,page,limit);
        Long count = Long.valueOf(shelvesList.size());
        return new ResponseData(shelvesList,count);
    }

    @Override
    public Long selectbyno(Long i) {
        return shelvesMapper.countShelvesByNo(i);
    }

    @Override
    public Integer addShelves(Shelves shelves) {
        int j=0;
        j=shelvesMapper.insert(shelves);
        return j;
    }

    @Override
    public ResponseData updateById(Shelves shelves) {
        try {
            Shelves shelves1 = shelvesMapper.selectByPrimaryKey(shelves.getId());
            //还需要加逻辑判定  -- 去学习正则表达式
            if(shelves1!=null){
                int i =  shelvesMapper.updateByPrimaryKey(shelves);
                if(i==0){
                    return new ResponseData(ExceptionMsg.FAILD);
                }else{
                    return new ResponseData(ExceptionMsg.SUCCESS);
                }
            }else {
                return new ResponseData("000407","请刷新以后在尝试！");
            }

        }catch (Exception e){
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return new ResponseData(ExceptionMsg.FAILD);
        }
    }

    @Override
    public List<Shelves> selectAll() {
        return shelvesMapper.selectAll();
    }

    @Override
    public Shelves select(Long j) {
        return shelvesMapper.select(j);
    }

    @Override
    public Integer addShelvesNum(long shelvesNo,Long shelvesonNum) {
        int j=0;
        j=shelvesMapper.addShelvesNum(shelvesNo,shelvesonNum);
        return j;
    }
}
